package ils.aquisition.web.action;

import ils.domain.acquisition.model.OrderItem;
import ils.web.model.WOrder;
import ils.services.AquisitionService;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.opensymphony.xwork2.ActionSupport;

public class PlaceOrderAction extends ActionSupport
{
    private static final long serialVersionUID = 1L;
    private int sellerId;
    private Map<Integer, OrderItem> items = new HashMap<Integer, OrderItem>();
    private String msg;
    private AquisitionService aquisitionService;
    public String execute() throws Exception
    {
        if(sellerId == 0 || items == null)
        {
            msg = "seller Id and book details needs to provided";
            return INPUT;
        }else
        {
            WOrder order = new WOrder();
            order.setSellerId(sellerId);
            Set<OrderItem> orderItems = new HashSet<OrderItem>(items.values());
            order.setItems(orderItems);
            aquisitionService.placeOrder(order);
            msg = "order successfully placed";
            return SUCCESS;
        }
    }
    public int getSellerId()
    {
        return sellerId;
    }
    public void setSellerId(int sellerId)
    {
        this.sellerId = sellerId;
    }
    public Map<Integer, OrderItem> getItems()
    {
        return items;
    }
    public void setItems(Map<Integer, OrderItem> items)
    {
        this.items = items;
    }
    public String getMsg()
    {
        return msg;
    }
    public void setAquisitionService(AquisitionService aquisitionService)
    {
        this.aquisitionService = aquisitionService;
    }
}
